<div class="modal-body">
	<form id="user-form" name="user-form" class="form-horizontal">
		<input type="hidden" name="id" id="userId">
		<input type="hidden" name="createTime" data-flag="date" data-format="yyyy-MM-dd HH:mm:ss">
		<input type="hidden" name="deleted">
		<div class="box-body" style="height:400px">
			<div class="col-md-5" style="border:1px solid #DDD;height:400px;margin-left:20px;OVERFLOW-Y: auto; OVERFLOW-X:hidden;">
				<ul id="source" class="ztree"></ul>
			</div>
			<div class="col-md-1" style="width:20px">
				
			</div>
			<div class="col-md-6" style="border:1px solid #DDD;height:400px">
				<table>
				<thead>
					<tr height='30'>
						<td width="100" align='center'>類別</td>
						<td width="200" align='center'>名稱</td>
					</tr>
				</thead>
				<tbody id="chooseValue" >
				
				</tbody>
				</table>
			</div>
		</div>
		<br/>
	</form>
</div>
<script type="text/javascript">
	var zNodes = new Array();
	var login_name = null;
	setting = {};
	
	$(document).ready( function(){
		//以后改到后台去一次性取出来
    	var url = "../../tbl_user_datapermission/listAllTree";
		$.getJSON( url, null, function( json ){
			//向页面上加载信息
			if( json && json.data ){
				zNodes = json.data;
				zTreeObj = $.fn.zTree.init( $("#source"), setting, zNodes );
			}			
		});
		
		if( Dtable_userInfo.rows('.selected').data().length > 0 ){
			login_name = Dtable_userInfo.rows('.selected').data()[0].login_name;
			refreshPermission( login_name );
		}
		
		styleRow();
		
	});
	
	function styleRow(){
		$("[name='perm_row']").click(function(){
			deletePermission( this.id );
		});
		$("[name='perm_row']").mouseover(function(){
			$(this).css("background-color","red");
			$(this).css("color","white");
		});
		$("[name='perm_row']").mouseout(function(){
			$(this).css("background-color","white");
			$(this).css("color","black");
		});
	}
	function dialog_close(){
		global.dialog.close()
	}
	
	function addPermission(event, treeId, treeNode){
		var code = treeNode.code;
        savePermission( code );
	}
	
	function savePermission( permission ){
		//从datatable上获取已经选择的条目
		if( Dtable_userInfo.rows('.selected').data().length > 0 ){
			login_name = Dtable_userInfo.rows('.selected').data()[0].login_name;
			var url = "../../tbl_user_datapermission/save";
			var paramMap = {};
			paramMap.object_code = login_name;
			paramMap.object_type = "USER";
			paramMap.permission = permission;
			var json_data = JSON.stringify( paramMap );
			$.ajax({
				  url: url,
				  type : 'post',
				  async : false,
				  data: json_data,
				  contentType: "application/json; charset=utf-8",
				  dataType: "json",
				  success: function( json ) {
					  refreshPermission( login_name );
				  }
			});
		}
	}
	
	function deletePermission( id ){
		var url = "../../tbl_user_datapermission/" + id;
		$.ajax({
			  url: url,
			  type : 'delete',
			  async : false,
			  contentType: "application/json; charset=utf-8",
			  dataType: "json",
			  success: function(json) {
				  refreshPermission( login_name );
			  }
		});
	}
	
	function refreshPermission( login_name ){
		var url = "../../tbl_user_datapermission/list";
		var paramMap = {};
		paramMap.object_code = login_name;
		paramMap.object_type = "USER";
		var json_data = JSON.stringify( paramMap );
		$.ajax({
			  url: url,
			  type : 'post',
			  async : false,
			  data: json_data,
			  contentType: "application/json; charset=utf-8",
			  dataType: "json",
			  success: function( json ) {
				  if( json.data ){
					  $("#chooseValue").html("");
					  $(json.data).each(function(){
						  var typename = null;
						  if("PROVINCE" == this.perm_level ){
							  typename = "省份";
						  }else if("CITY" == this.perm_level ){
							  typename = "城市";
						  }else if("COUNTY" == this.perm_level ){
							  typename = "地区";
						  }else if("PROJECT" == this.perm_level ){
							  typename = "项目";
						  }else if("ZHT" == this.perm_level ){
							  typename = "灾害体";
						  }else if("STATION" == this.perm_level ){
							  typename = "站点";
						  }
						  $str="";
					      $str+="<tr style='cursor:pointer' name='perm_row' id='"+ this.id +"' align='center' height='25' >";
					      $str+="<td>"+ typename +"</td>";
					      $str+="<td>"+ this.description +"</td>";
					      $str+="</tr>";
					      $("#chooseValue").append($str);
					        
					      styleRow();
					  });
				  }
			  }
		});
	}
	
	var setting = {  
	    data: {  
	        simpleData: {  
	            enable: true  
	        }  
	    } ,
	    callback:{
    		onClick: addPermission
    	},
	    async: {
	        enable: true,
	        contentType: "application/json; charset=utf-8",
			dataType: "json",
	        url: "../../tbl_area/ztree",
	        autoParam:["id", "name", "level"],
	        otherParam:{"otherParam":"zTreeAsyncTest"},
	        dataFilter: filter
	    }
	};
	
	
    $(".sm-left>.tree").niceScroll({
        cursorcolor:"#999",
        cursoropacitymax:1,
        touchbehavior:false,
        cursorwidth:"6px",
        cursorborder:"0",
        cursorborderradius:"5px",
        autohidemode:false,
    });
    
    
    function filter( treeId, parentNode, childNodes ) {
	    if (!childNodes.data) return null;
	    for (var i=0, l=childNodes.data.length; i<l; i++) {
	    	childNodes.data[i].name = childNodes.data[i].name.replace(/\.n/g, '.');
	    	childNodes.data[i].id = childNodes.data[i].code;
	    	childNodes.data[i].pId = childNodes.data[i].p_code;
	    	childNodes.data[i].iconSkin = childNodes.data[i].iconSkin.replace("close","docu")
	    }
	    return childNodes.data;
	}
    
</script>